package com.hdphone.zljutils.impl;

import com.hdphone.zljutils.inter.ICpuUtil;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CpuUtilImpl implements ICpuUtil {

    private final static String kCpuInfoMaxFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";

    private final static String kCpuInfoMinFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq";

    private final static String kCpuInfoCurFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq";


    @Override
    public String getMaxCpuFreq() {
        String result = "";
        FileReader fr = null;
        BufferedReader br = null;

        try {
            fr = new FileReader(kCpuInfoMaxFreqFilePath);
            br = new BufferedReader(fr);
            String text = br.readLine();
            result = text;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return result;

    }

    @Override
    public String getMinCpuFreq() {
//        File file = new File("/sys/devices/system/cpu/cpu0");
//        if (file.exists()){
//            System.out.println("exist");
//        }

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(kCpuInfoMinFreqFilePath);
            br = new BufferedReader(fr);

            String text = br.readLine();
            return text;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return null;
    }

    @Override
    public String getCurCpuFreq() {
        FileReader fr = null;
        BufferedReader br = null;


        try {
            fr = new FileReader(kCpuInfoCurFreqFilePath);

            br = new BufferedReader(fr);
            String text = br.readLine();

            return text;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }


        return null;


    }

    @Override
    public String getCpuName() {
        FileReader fr = null;
        BufferedReader br = null;

        try {
            //Hardware	: Qualcomm Technologies, Inc SM8150P
            fr = new FileReader("/proc/cpuinfo");
            br = new BufferedReader(fr);
            String text = br.readLine();
            while (text == null || !text.startsWith("Hardware")) {
                text = br.readLine();
            }

            String[] array = text.split(":\\s+", 2);

            if (array.length == 2) {
                return array[1].trim();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        return null;

    }
}
